<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"sekla.cn","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="之前写的，现在看代码惨不忍睹。 实验目的基于教材内容，从顺序查找、二分查找、基于BST的查找和哈希中任选两种查找算法，实现并比较性能。">
<meta property="og:type" content="article">
<meta property="og:title" content="一个查找比较试验">
<meta property="og:url" content="http://sekla.cn/2021/04/14/dslab8/index.html">
<meta property="og:site_name" content="Sekla&#39;s Blog">
<meta property="og:description" content="之前写的，现在看代码惨不忍睹。 实验目的基于教材内容，从顺序查找、二分查找、基于BST的查找和哈希中任选两种查找算法，实现并比较性能。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-14T04:39:04.000Z">
<meta property="article:modified_time" content="2021-10-18T14:44:40.444Z">
<meta property="article:author" content="Sekla">
<meta property="article:tag" content="C++">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://sekla.cn/2021/04/14/dslab8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>一个查找比较试验 | Sekla's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script type="text/javascript" src="/js/my_js/clicklove.js"></script>
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Sekla's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Keep Learning Keep Doing</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">100</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://sekla.cn/2021/04/14/dslab8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Sekla">
      <meta itemprop="description" content="Sekla">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Sekla's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          一个查找比较试验
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-04-14 12:39:04" itemprop="dateCreated datePublished" datetime="2021-04-14T12:39:04+08:00">2021-04-14</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-18 22:44:40" itemprop="dateModified" datetime="2021-10-18T22:44:40+08:00">2021-10-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">数据结构与算法</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>11k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>10 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>之前写的，现在看代码惨不忍睹。</p>
<h2 id="实验目的"><a href="#实验目的" class="headerlink" title="实验目的"></a>实验目的</h2><p>基于教材内容，从顺序查找、二分查找、基于BST的查找和哈希中任选两种查找算法，实现并比较性能。</p>
<a id="more"></a>

<h3 id="基本要求"><a href="#基本要求" class="headerlink" title="基本要求"></a>基本要求</h3><p>（1）对实现的查找算法进行实验比较，在不同数据规模（N）下执行100次成功查找，以表格形式记录最小、最大和平均查找时间；在不同数据规模（N）下执行100次不成功查找，以表格形式记录最小、最大和平均查找时间。</p>
<p>（2）查找算法要基于教材，测试输入的整数数据文件（5个，文件中数据规模N分别是100，1K，10K，100K和1M），每次查找的比较次数和时间也要输出到文件中。</p>
<p>（3）提交最终实验作业。用附件的形式，提交两个文件：一个压缩包（包含源码和5个用于查找测试的数据文件）;一个pdf文档（文档中包含实验日志和一个根据基本要求（1）记录实验结果的表格，然后进行适当的实验结果分析）。</p>
<p>本次实验我选择了哈希查找与二分查找。</p>
<p>首先需要了解<strong>二分查找</strong>的具体含义：二分查找必须用顺序存储结构进行数据存储，即查找前待测表必须为有序，这是他的缺点，但是优点是每次查找进行关键字中间位置的比较，依次二分下去，直到查找成功或查找不成功。二分查找需要使用顺序结构，这里直接选择数组。</p>
<p><strong>哈希</strong>是通过哈希函数对于关键字的映射得到哈希地址建立哈希表，所以每次查找只需要知道哈希函数即可直接取得所查记录，但是同一哈希函数必定会得到冲突的哈希地址，所以现在选取什么哈希函数来建立哈希表成为一个问题。</p>
<h3 id="1-编写建立测试数据函数"><a href="#1-编写建立测试数据函数" class="headerlink" title="1.编写建立测试数据函数"></a>1.编写建立测试数据函数</h3><p>通过C++11random库进行随机数生成，详见上一篇随机数生成。</p>
<h3 id="2-编写查找函数代码"><a href="#2-编写查找函数代码" class="headerlink" title="2.编写查找函数代码"></a>2.编写查找函数代码</h3><p>二分查找需要有序顺序表，所以先对数据进行排序,再进行二分查找，二分查找还有许多实现细节，参考</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/xiao_jj_jj/article/details/106018702">https://blog.csdn.net/xiao_jj_jj/article/details/106018702</a></p>
<p>哈希查找运用线性哈希，直接写代码。计时功能运用C++11高精度计时库chrono，头文件<chrono>。</p>
<h3 id="3-具体代码"><a href="#3-具体代码" class="headerlink" title="3.具体代码"></a>3.具体代码</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;ctime&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;random&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;chrono&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> minn = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> max0 = <span class="number">1e7</span>;</span><br><span class="line"><span class="keyword">bool</span> hashtable[<span class="number">10</span> * max0 + <span class="number">5</span>] = &#123; <span class="number">0</span> &#125;;</span><br><span class="line"><span class="keyword">int</span>* arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">1000005</span>]();</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Init</span><span class="params">(fstream&amp; in, <span class="keyword">int</span> A[])</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> temp = <span class="number">0</span>, cnt = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (!in.eof() &amp;&amp; cnt &lt; max0) &#123;</span><br><span class="line">        in &gt;&gt; temp;</span><br><span class="line">        A[cnt] = temp;</span><br><span class="line">        cnt++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//从文件输入</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Init</span><span class="params">(fstream&amp; in, <span class="keyword">int</span> arr[], <span class="keyword">bool</span> hash[])</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> temp=<span class="number">0</span>, cnt=<span class="number">0</span>, key = <span class="number">0</span>;</span><br><span class="line">    fill(hash, hash + max0, <span class="number">0</span>);</span><br><span class="line">    <span class="keyword">while</span> ((!in.eof()) &amp;&amp; cnt &lt; max0) &#123;</span><br><span class="line">        in &gt;&gt; temp;</span><br><span class="line">        arr[cnt] = temp;</span><br><span class="line">        key = temp % max0;</span><br><span class="line">        hash[key] = <span class="number">1</span>;<span class="comment">//线性hash</span></span><br><span class="line">        cnt++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//创建测试数据</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">createTestData</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> default_random_engine ge;<span class="comment">//创建引擎</span></span><br><span class="line">    ge.seed(time(<span class="number">0</span>));</span><br><span class="line">    <span class="function"><span class="keyword">static</span> uniform_int_distribution&lt;<span class="keyword">int</span>&gt; <span class="title">dis</span><span class="params">(minn, max0)</span></span>;<span class="comment">//创建取值范围</span></span><br><span class="line">    <span class="comment">//让每次生成的随机数不同，设置种子(定义前后均可设置种子)</span></span><br><span class="line">    <span class="comment">//default_random_engin ge2(12345);创建引擎设置</span></span><br><span class="line">    <span class="comment">/*default_random_engine ge3;</span></span><br><span class="line"><span class="comment">    ge3.seed(time(0));随机种子*/</span></span><br><span class="line">    <span class="comment">//定义成static多次调用同一对范围和引擎使得每一次生成的数不一样。</span></span><br><span class="line">    <span class="keyword">int</span> maxn = max0 / <span class="number">10</span>;</span><br><span class="line">    <span class="comment">//数组引索随机数</span></span><br><span class="line">    <span class="function">uniform_int_distribution&lt;<span class="keyword">int</span>&gt; <span class="title">dis1</span><span class="params">(minn, maxn)</span></span>;</span><br><span class="line">    <span class="built_in">string</span> files[<span class="number">7</span>] = &#123; <span class="string">&quot;100.txt&quot;</span>,<span class="string">&quot;1k.txt&quot;</span>,<span class="string">&quot;10k.txt&quot;</span>,<span class="string">&quot;100k.txt&quot;</span>,<span class="string">&quot;1M.txt&quot;</span>,<span class="string">&quot;success.txt&quot;</span>,<span class="string">&quot;fail.txt&quot;</span> &#125;;</span><br><span class="line">    fstream file;</span><br><span class="line">    <span class="comment">//产生1M随机数</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; maxn; i++) &#123;</span><br><span class="line">        arr[i] = dis(ge);</span><br><span class="line">        hashtable[arr[i]] = <span class="number">1</span>;<span class="comment">//已有数</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//生成100个随机数同时将这组数据作为成功查找样例</span></span><br><span class="line">    file.open(files[<span class="number">0</span>], ios::out);</span><br><span class="line">    <span class="keyword">int</span>* arr100 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">100</span>]();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100</span>; i++) &#123;</span><br><span class="line">        arr100[i] = arr[dis1(ge)];</span><br><span class="line">        file &lt;&lt; arr100[i] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    file.close();</span><br><span class="line">    <span class="comment">//success</span></span><br><span class="line">    file.open(files[<span class="number">5</span>], ios::out);</span><br><span class="line">    <span class="keyword">int</span>* success = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">100</span>]();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100</span>; i++) &#123;</span><br><span class="line">        success[i] = arr100[i];</span><br><span class="line">        file &lt;&lt; arr100[i] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">delete</span>[] arr100;</span><br><span class="line">    file.close();</span><br><span class="line">    <span class="comment">//1k</span></span><br><span class="line">    file.open(files[<span class="number">1</span>], ios::out);</span><br><span class="line">    <span class="keyword">int</span>* arr1k = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">1000</span>]();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">1000</span>; i++) &#123;<span class="comment">//平均散布100个样例</span></span><br><span class="line">        <span class="keyword">if</span> (i % <span class="number">10</span> == <span class="number">0</span> &amp;&amp; i != <span class="number">0</span>) &#123;</span><br><span class="line">            arr1k[i] = success[i / <span class="number">10</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> arr1k[i] = arr[dis1(ge)];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">1000</span>; i++)</span><br><span class="line">        file &lt;&lt; arr1k[i] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">delete</span>[] arr1k;</span><br><span class="line">    file.close();</span><br><span class="line">    <span class="comment">//10k</span></span><br><span class="line">    file.open(files[<span class="number">2</span>], ios::out);</span><br><span class="line">    <span class="keyword">int</span>* arr10k = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">10000</span>]();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10000</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (i % <span class="number">10</span> == <span class="number">0</span> &amp;&amp; i != <span class="number">0</span>) &#123;</span><br><span class="line">            arr10k[i] = success[i / <span class="number">100</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> arr10k[i] = arr[dis1(ge)];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10000</span>; i++)</span><br><span class="line">        file &lt;&lt; arr10k[i] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">delete</span>[] arr10k;</span><br><span class="line">    file.close();</span><br><span class="line">    <span class="comment">//100k</span></span><br><span class="line">    file.open(files[<span class="number">3</span>], ios::out);</span><br><span class="line">    <span class="keyword">int</span>* arr100k = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">100000</span>]();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100000</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (i % <span class="number">10</span> == <span class="number">0</span> &amp;&amp; i != <span class="number">0</span>) &#123;</span><br><span class="line">            arr100k[i] = success[i / <span class="number">1000</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> arr100k[i] = arr[dis1(ge)];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100000</span>; i++)</span><br><span class="line">        file &lt;&lt; arr100k[i] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">delete</span>[] arr100k;</span><br><span class="line">    file.close();</span><br><span class="line">    <span class="comment">//1M</span></span><br><span class="line">    file.open(files[<span class="number">4</span>], ios::out);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">1000000</span>; i++)</span><br><span class="line">        file &lt;&lt; arr[i] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    file.close();</span><br><span class="line">    <span class="comment">//失败查找数据文件,即没有产生的数据且在成功数据集范围内.</span></span><br><span class="line">    file.open(files[<span class="number">6</span>], ios::out);</span><br><span class="line">    <span class="keyword">int</span> cnt = <span class="number">0</span>, number = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (cnt &lt; <span class="number">100</span>) &#123;</span><br><span class="line">        number = dis(ge);</span><br><span class="line">        <span class="keyword">if</span> (hashtable[number] == <span class="number">0</span>) &#123;</span><br><span class="line">            file &lt;&lt; number &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            cnt++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    file.close();</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;测试数据创建完毕&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;------------------------------------------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//bianry search</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">bin_search</span><span class="params">(fstream&amp; out, <span class="keyword">int</span> arr[], <span class="keyword">int</span> n, <span class="keyword">int</span> success[], <span class="keyword">int</span> fail[])</span> </span>&#123;</span><br><span class="line">    <span class="keyword">using</span>  <span class="keyword">namespace</span> <span class="built_in">std</span>::chrono;</span><br><span class="line">    sort(arr, arr + n);<span class="comment">//排序</span></span><br><span class="line">    <span class="keyword">int</span> cnt, num = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> left = <span class="number">0</span>, right = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">bool</span> flag = <span class="number">0</span>;<span class="comment">//查找成功值</span></span><br><span class="line">    <span class="keyword">double</span> mints = <span class="number">100.0</span>, maxts = <span class="number">0</span>, averages = <span class="number">0</span>, mintf = <span class="number">100.0</span>, maxtf = <span class="number">0</span>, averagef = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">200</span>; i++) &#123;</span><br><span class="line">        <span class="comment">//查找成功与失败样例</span></span><br><span class="line">        <span class="keyword">if</span> (i &lt; <span class="number">100</span>) &#123;</span><br><span class="line">            num = success[i % <span class="number">100</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> num = fail[i % <span class="number">100</span>];</span><br><span class="line">        cnt = <span class="number">0</span>;</span><br><span class="line">        left = <span class="number">0</span>, right = n - <span class="number">1</span>;</span><br><span class="line">        flag = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">auto</span> start = steady_clock::now();</span><br><span class="line">        <span class="keyword">while</span> (left &lt;= right) &#123;</span><br><span class="line">            cnt++;</span><br><span class="line">            <span class="keyword">int</span> mid = left + (right - left) / <span class="number">2</span>;<span class="comment">//比较后重置中间值,计算mid防溢出</span></span><br><span class="line">            <span class="keyword">if</span> (arr[mid] == num) &#123;</span><br><span class="line">                flag = <span class="number">1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (arr[mid] &gt; num) right = mid - <span class="number">1</span>;<span class="comment">//重设边界</span></span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (arr[mid] &lt; num) left = mid + <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">auto</span> end = steady_clock::now();</span><br><span class="line">        duration&lt;<span class="keyword">double</span>&gt; elapsed = end - start;</span><br><span class="line">        <span class="keyword">if</span> (flag == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;二分查找&quot;</span> &lt;&lt; num &lt;&lt; <span class="string">&quot;成功 	&quot;</span> &lt;&lt; <span class="string">&quot;查找次数:&quot;</span> &lt;&lt; cnt &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            out &lt;&lt; <span class="string">&quot;二分查找&quot;</span> &lt;&lt; num &lt;&lt; <span class="string">&quot;成功 	&quot;</span> &lt;&lt; <span class="string">&quot;查找次数:&quot;</span> &lt;&lt; cnt &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            mints = min(mints, (<span class="keyword">double</span>)elapsed.count());</span><br><span class="line">            maxts = max(maxts, (<span class="keyword">double</span>)elapsed.count());<span class="comment">//最大时间</span></span><br><span class="line">            averages = averages + (<span class="keyword">double</span>)elapsed.count() / <span class="number">100</span>;<span class="comment">//平均时间</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (flag == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;二分查找&quot;</span> &lt;&lt; num &lt;&lt; <span class="string">&quot;失败 	&quot;</span> &lt;&lt; <span class="string">&quot;查找次数:&quot;</span> &lt;&lt; cnt &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            out &lt;&lt; <span class="string">&quot;二分查找:&quot;</span> &lt;&lt; num &lt;&lt; <span class="string">&quot;失败 	&quot;</span> &lt;&lt; <span class="string">&quot;查找次数:&quot;</span> &lt;&lt; cnt &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            mintf = min(mintf, (<span class="keyword">double</span>)elapsed.count());</span><br><span class="line">            maxtf = max(maxtf, (<span class="keyword">double</span>)elapsed.count());</span><br><span class="line">            averagef = averagef + (<span class="keyword">double</span>)elapsed.count() / <span class="number">100</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;查找时间:&quot;</span> &lt;&lt; (<span class="keyword">double</span>)elapsed.count() &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        out &lt;&lt; <span class="string">&quot;查找时间:&quot;</span> &lt;&lt; (<span class="keyword">double</span>)elapsed.count() &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;--------------------------------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;二分查找成功;&quot;</span> &lt;&lt; <span class="string">&quot;最小查找时间=&quot;</span> &lt;&lt; mints &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;   &quot;</span> &lt;&lt; <span class="string">&quot;最大查找时间&quot;</span> &lt;&lt; maxts &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;平均查找时间&quot;</span> &lt;&lt; averages &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    out &lt;&lt; <span class="string">&quot;二分查找成功;&quot;</span> &lt;&lt; <span class="string">&quot;最小查找时间=&quot;</span> &lt;&lt; mints &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;   &quot;</span> &lt;&lt; <span class="string">&quot;最大查找时间&quot;</span> &lt;&lt; maxts &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;平均查找时间&quot;</span> &lt;&lt; averages &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;二分查找失败;&quot;</span> &lt;&lt; <span class="string">&quot;最小查找时间=&quot;</span> &lt;&lt; mintf &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;   &quot;</span> &lt;&lt; <span class="string">&quot;最大查找时间&quot;</span> &lt;&lt; maxtf &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;平均查找时间&quot;</span> &lt;&lt; averagef &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    out &lt;&lt; <span class="string">&quot;二分查找失败;&quot;</span> &lt;&lt; <span class="string">&quot;最小查找时间=&quot;</span> &lt;&lt; mintf &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;   &quot;</span> &lt;&lt; <span class="string">&quot;最大查找时间&quot;</span> &lt;&lt; maxtf &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;平均查找时间&quot;</span> &lt;&lt; averagef &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//hash search</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">hashsearch</span><span class="params">(fstream&amp; out, <span class="keyword">bool</span> h[], <span class="keyword">int</span> success[], <span class="keyword">int</span> fail[])</span> </span>&#123;</span><br><span class="line">    <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>::chrono;</span><br><span class="line">    <span class="keyword">double</span> mints = <span class="number">100.0</span>, maxts = <span class="number">0</span>, averages = <span class="number">0</span>, mintf = <span class="number">100.0</span>, maxtf = <span class="number">0</span>, averagef = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">bool</span> flag = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> test = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> cnt = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">200</span>; i++) &#123;</span><br><span class="line">        flag = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span> (i &lt; <span class="number">100</span>) &#123;</span><br><span class="line">            test = success[i % <span class="number">100</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            test = fail[i % <span class="number">100</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        time_point&lt;steady_clock&gt; start_h = steady_clock::now();</span><br><span class="line">        <span class="keyword">if</span> (h[test]) flag = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">else</span> flag = <span class="number">0</span>;</span><br><span class="line">        time_point&lt;steady_clock&gt; end_h = steady_clock::now();</span><br><span class="line">        duration&lt;<span class="keyword">double</span>&gt; elapsed_h = end_h - start_h;</span><br><span class="line">        <span class="keyword">if</span> (flag == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;哈希查找:&quot;</span> &lt;&lt; test &lt;&lt; <span class="string">&quot;成功 	&quot;</span> &lt;&lt; <span class="string">&quot;查找次数:&quot;</span> &lt;&lt; cnt &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            out &lt;&lt; <span class="string">&quot;哈希查找:&quot;</span> &lt;&lt; test &lt;&lt; <span class="string">&quot;成功 	&quot;</span> &lt;&lt; <span class="string">&quot;查找次数:&quot;</span> &lt;&lt; cnt &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            mints = min(mints, (<span class="keyword">double</span>)elapsed_h.count());</span><br><span class="line">            maxts = max(maxts, (<span class="keyword">double</span>)elapsed_h.count());<span class="comment">//最大时间</span></span><br><span class="line">            averages = averages + (<span class="keyword">double</span>)elapsed_h.count() / <span class="number">100</span>;<span class="comment">//平均时间</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (flag == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;哈希查找:&quot;</span> &lt;&lt; test &lt;&lt; <span class="string">&quot;失败 	&quot;</span> &lt;&lt; <span class="string">&quot;次数:&quot;</span> &lt;&lt; cnt &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            out &lt;&lt; <span class="string">&quot;哈希查找:&quot;</span> &lt;&lt; test &lt;&lt; <span class="string">&quot;失败 	&quot;</span> &lt;&lt; <span class="string">&quot;次数:&quot;</span> &lt;&lt; cnt &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            mintf = min(mintf, (<span class="keyword">double</span>)elapsed_h.count());</span><br><span class="line">            maxtf = max(maxtf, (<span class="keyword">double</span>)elapsed_h.count());</span><br><span class="line">            averagef = averagef + (<span class="keyword">double</span>)elapsed_h.count() / <span class="number">100</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;查找时间:&quot;</span> &lt;&lt; (<span class="keyword">double</span>)elapsed_h.count() &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        out &lt;&lt; <span class="string">&quot;查找时间:&quot;</span> &lt;&lt; (<span class="keyword">double</span>)elapsed_h.count() &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;--------------------------------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;哈希查找成功;&quot;</span> &lt;&lt; <span class="string">&quot;最小查找时间=&quot;</span> &lt;&lt; mints &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;   &quot;</span> &lt;&lt; <span class="string">&quot;最大查找时间&quot;</span> &lt;&lt; maxts &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;平均查找时间&quot;</span> &lt;&lt; averages &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    out &lt;&lt; <span class="string">&quot;哈希查找成功;&quot;</span> &lt;&lt; <span class="string">&quot;最小查找时间=&quot;</span> &lt;&lt; mints &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;   &quot;</span> &lt;&lt; <span class="string">&quot;最大查找时间&quot;</span> &lt;&lt; maxts &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;平均查找时间&quot;</span> &lt;&lt; averages &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;哈希查找失败;&quot;</span> &lt;&lt; <span class="string">&quot;最小查找时间=&quot;</span> &lt;&lt; mintf &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;   &quot;</span> &lt;&lt; <span class="string">&quot;最大查找时间&quot;</span> &lt;&lt; maxtf &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;平均查找时间&quot;</span> &lt;&lt; averagef &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    out &lt;&lt; <span class="string">&quot;哈希查找失败;&quot;</span> &lt;&lt; <span class="string">&quot;最小查找时间=&quot;</span> &lt;&lt; mintf &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;   &quot;</span> &lt;&lt; <span class="string">&quot;最大查找时间&quot;</span> &lt;&lt; maxtf &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="string">&quot;平均查找时间&quot;</span> &lt;&lt; averagef &lt;&lt; <span class="string">&quot;s&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    createTestData();</span><br><span class="line">    <span class="built_in">string</span> files[<span class="number">12</span>] = &#123; <span class="string">&quot;100.txt&quot;</span>,<span class="string">&quot;1k.txt&quot;</span>,<span class="string">&quot;10k.txt&quot;</span>,<span class="string">&quot;100k.txt&quot;</span>,<span class="string">&quot;1M.txt&quot;</span>,<span class="string">&quot;success.txt&quot;</span>,<span class="string">&quot;fail.txt&quot;</span>,<span class="string">&quot;100out.txt&quot;</span>,<span class="string">&quot;1kout.txt&quot;</span>,<span class="string">&quot;10kout.txt&quot;</span>,<span class="string">&quot;100kout.txt&quot;</span>,<span class="string">&quot;1Mout.txt&quot;</span> &#125;;</span><br><span class="line">    <span class="keyword">int</span>* su = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">105</span>]();</span><br><span class="line">    <span class="keyword">int</span>* fa = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">105</span>]();</span><br><span class="line">    fstream file;</span><br><span class="line">    file.open(files[<span class="number">5</span>], ios::in);</span><br><span class="line">    Init(file, su);</span><br><span class="line">    file.close();</span><br><span class="line">    file.open(files[<span class="number">6</span>], ios::in);</span><br><span class="line">    Init(file, fa);</span><br><span class="line">    file.close();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">        file.open(files[i], ios::in);</span><br><span class="line">        Init(file, arr, hashtable);</span><br><span class="line">        file.close();</span><br><span class="line">        file.open(files[i + <span class="number">7</span>], ios::out);</span><br><span class="line">        <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;数据规模为&quot;</span> &lt;&lt; <span class="number">100</span> * <span class="built_in">pow</span>(<span class="number">10</span>, i) &lt;&lt; <span class="string">&quot;哈希查找测试数据:&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        file &lt;&lt; <span class="string">&quot;数据规模为&quot;</span> &lt;&lt; <span class="number">100</span> * <span class="built_in">pow</span>(<span class="number">10</span>, i) &lt;&lt; <span class="string">&quot;哈希查找数据测试:&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        hashsearch(file, hashtable, su, fa);</span><br><span class="line">        <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;数据规模为&quot;</span> &lt;&lt; <span class="number">100</span> * <span class="built_in">pow</span>(<span class="number">10</span>, i) &lt;&lt; <span class="string">&quot;二分查找数据测试:&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        file &lt;&lt; <span class="string">&quot;数据规模为&quot;</span> &lt;&lt; <span class="number">100</span> * <span class="built_in">pow</span>(<span class="number">10</span>, i) &lt;&lt; <span class="string">&quot;二分查找数据测试:&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        bin_search(file, arr, <span class="number">100</span> * <span class="built_in">pow</span>(<span class="number">10</span>, i), su, fa);</span><br><span class="line">        file.close();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">delete</span>[] arr;</span><br><span class="line">    <span class="keyword">delete</span>[] su;</span><br><span class="line">    <span class="keyword">delete</span>[] fa;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>本实验代码参考学长代码并进行大幅度改良，测试数据更加平均，且计时与随机数生成部分用C++11重写,参考代码:</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_44307065/article/details/103441948">https://blog.csdn.net/weixin_44307065/article/details/103441948</a></p>

    </div>
    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/C/" rel="tag"><i class="fa fa-tag"></i> C++</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/14/datas-tree/" rel="prev" title="树的一些总结">
      <i class="fa fa-chevron-left"></i> 树的一些总结
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/14/C++11RANDOM/" rel="next" title="C++随机数大总结">
      C++随机数大总结 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E9%AA%8C%E7%9B%AE%E7%9A%84"><span class="nav-number">1.</span> <span class="nav-text">实验目的</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E8%A6%81%E6%B1%82"><span class="nav-number">1.1.</span> <span class="nav-text">基本要求</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E7%BC%96%E5%86%99%E5%BB%BA%E7%AB%8B%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%E5%87%BD%E6%95%B0"><span class="nav-number">1.2.</span> <span class="nav-text">1.编写建立测试数据函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E7%BC%96%E5%86%99%E6%9F%A5%E6%89%BE%E5%87%BD%E6%95%B0%E4%BB%A3%E7%A0%81"><span class="nav-number">1.3.</span> <span class="nav-text">2.编写查找函数代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%85%B7%E4%BD%93%E4%BB%A3%E7%A0%81"><span class="nav-number">1.4.</span> <span class="nav-text">3.具体代码</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Sekla"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Sekla</p>
  <div class="site-description" itemprop="description">Sekla</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">100</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/ShenTiao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ShenTiao" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:584892986@qq.com" title="E-Mail → mailto:584892986@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

      <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1416875904&auto=0&height=66"></iframe>
      
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Sekla</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">288k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:22</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":250,"height":500},"mobile":{"show":false},"react":{"opacity":0.9},"log":false});</script></body>
</html>

<script type="text/javascript" src="/js/src/love.js"></script>
